//
//  stats_container.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// 


#ifndef STATS_CONTAINER_HPP
#define STATS_CONTAINER_HPP
#include <string>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include "stats_server/stats.pb.h"

class stats_container
{
    typedef std::multimap<std::string,stats_message_t> stats_map_t;
    boost::shared_ptr<stats_map_t> stats_map_ptr;
    boost::mutex mutex_;
    boost::asio::deadline_timer timer_;
    std::string persist_path_;
    boost::posix_time::minutes expected_lifetime_;
    void handle_timeout(boost::system::error_code const&ec);
public:
    stats_container(boost::asio::io_service& ios,std::string const & persist_path,boost::posix_time::minutes expected_lifetime);
    bool add(stats_message_t const& m);
    bool get(std::string mkey, std::vector<stats_message_t>& m);

    int purge_old();
    bool load();
    bool save();
    bool shutdown();
};
std::string message_key(stats_message_t const & m);

#endif // STATS_CONTAINER_HPP
